'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview boss
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var _ = require('lodash');
var dataApi = require('../libs/dataApi');
var genRewardService = require('../services/genRewardService');

exports.getSkillList = function (boss_id) {
    var skill_list = [];
    var boss_skill_all_conf = dataApi.boss_skill.findById(boss_id);
    if (!boss_skill_all_conf) {
        return skill_list;
    }

    _.each(boss_skill_all_conf, function (boss_skill_conf) {
        var skill_conf = dataApi.boss_skill_config.findById(boss_skill_conf.skill_id);
        skill_list.push({
            skill_id: skill_conf.id,
            atk_type: skill_conf.atk_type,
            skill_range: skill_conf.skill_range,
            skill_time: skill_conf.skill_time,
            time_space: skill_conf.time_space,
            value: skill_conf.value
        });
    });
    return skill_list;
};

/**
 * 读取Boss伤害排行的奖励配置
 */
exports.getBossRewardConf = function (job_type, rank) {
    var boss_rank_item = dataApi.boss_rank.find(function (bri) {
        return rank >= bri.min_cup && rank <= bri.max_cup;
    });

    var reward = dataApi.boss_rank_reward.findById(boss_rank_item.id);
    var reward_list = [];
    _.each(reward, function (reward_item) {
        if (reward_item.job_type === job_type) {
            reward_list.push(genRewardService.generateReward(reward_item));
        }
    });
    return reward_list;
};

/**
 * 随机出一个BOSS
 * @return {[type]} [description]
 */
exports.getRandBossId = function () {
    var rand_num = _.random(dataApi.boss_type.length() - 1);
    var rand_boss = null;
    var pos_num = 0;
    dataApi.boss_type.any(function (item) {
        pos_num++;
        if (rand_num < pos_num) {
            rand_boss = item;
            return true;
        }
    });
    return rand_boss.id;
};

/**
 * 获取当前的boss配置
 * @return {[type]} [description]
 */
exports.getTodayBossConf = function () {
    return _getTodayBossConf();
};

var _getTodayBossConf = function () {
    var week_day = moment().isoWeekday();
    return dataApi.boss_date.findById(week_day);
};
